#!/usr/bin/perl -w

use Test::More tests => 104;
use Test::Exception;
use Cwd qw/abs_path/;
use blib;
use strict;

use Data::Dump qw/dump/;

BEGIN {
use_ok( 'WebPAC::Input::ISIS' );
use_ok( 'WebPAC::Input::MARC' );
}

my $debug = shift @ARGV;
my $no_log = $debug ? 0 : 1;

ok(my $abs_path = abs_path($0), "abs_path");
$abs_path =~ s#/[^/]*$#/#;

my $module = 'WebPAC::Input::ISIS';
diag "testing with $module";

throws_ok { my $input = new WebPAC::Input( ) } qr/module/, "need module";
ok(my $input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1, stats => 1 ), "new $module");
ok(my $input_lm = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");

throws_ok { $input->open( ) } qr/path/, "need path";

throws_ok { $input->open( path => '/dev/null', ) } qr/can't find database/ , "open";

my $store;

ok($input->open( path => "$abs_path/winisis/BIBL" ), "open winisis");
ok($input_lm->open(
	path => "$abs_path/winisis/BIBL",
	save_row => sub {
		my $a = shift;
		$store->{ $a->{id} } = $a->{row};
	},
	load_row => sub {
		my $a = shift;
		return defined($store->{ $a->{id} }) &&
			$store->{ $a->{id} };
	},
), "open winisis");

cmp_ok( keys %$store, '==', 5, 'have 5 rows');

foreach my $i ( 1 .. 5 ) {
	ok(my $r = $store->{$i}, "row $i");
	ok($r->{'000'}, "have 000");
	isa_ok($r->{'000'}, 'ARRAY', "is ARRAY");
	cmp_ok($r->{'000'}->[0], '==', $i, 'sane value');
}

diag "store = ",dump( $store ) if ($debug);

sub test_after_open($) {
	my $input = shift;

	cmp_ok($input->pos, '==', -1, "mfn");
	ok(my $size = $input->size, "size");
	return $size;
}

test_after_open($input);
my $size = test_after_open($input_lm);

sub test_fetch($$) {
	my ($input, $size) = @_;

	my @db;

	foreach my $mfn ( 1 ... $size ) {
		ok(my $rec = $input->fetch, "fetch $mfn");
		cmp_ok($input->pos, '==', $mfn, "pos $mfn");
		push @db, $rec;
		ok(my $dump = $input->dump_ascii, "dump_ascii $mfn");
		diag $dump if ($debug);
	}

	return @db;
}

my @db1 = test_fetch($input, $size);
my @db2 = test_fetch($input_lm, $size);

is_deeply(\@db1, \@db2, "seek working");

sub test_start_limit($$$$) {
	my ($input, $s,$l,$e) = @_;

	diag "offset $s, limit: $l, expected: $e";

	ok($s = $input->open( path => "$abs_path/winisis/BIBL", offset => $s, limit => $l, debug => $debug ), "open winisis");
	cmp_ok($s, '==', $size, "db size from open = $size");
	cmp_ok($input->size, '==', $e, "input->size = $e");
}

test_start_limit($input, 1, 3, 3);
test_start_limit($input, $size, 3, 0);
test_start_limit($input, 3, $size, $size - 2);
test_start_limit($input, 1, $size + 2, $size);

ok(my $s = $input->stats, 'stats');
diag "stats:\n$s";

$module = 'WebPAC::Input::MARC';
diag "testing with $module";

ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");

ok($input->open( path => "$abs_path/data/marc.iso" ), "open marc.iso");

test_after_open($input);

test_fetch($input, $input->size);

# test modify_record
$module = 'WebPAC::Input::ISIS';
ok($input = new WebPAC::Input( module => $module, no_log => $no_log, no_progress_bar => 1 ), "new $module");

ok($input->open( path => "$abs_path/modify_isis/LIBRI", ), "open modify_isis (plain)");
ok(my $rec_p = $input->fetch, 'fetch');

ok($input->open(
	path => "$abs_path/modify_isis/LIBRI",
	modify_records => {
		200 => {
			'*' => { '^c' => '. ' },
		}
	},
), "open modify_isis (with modify_records)");

ok(my $rec = $input->fetch, 'fetch');

cmp_ok($rec_p->{200}->[0]->{f} . '. ' . $rec_p->{200}->[0]->{c}, 'eq' ,$rec->{200}->[0]->{f}, 'modify_records working');

